import environ, agent
from config import Config
import pygame, time
from pygame.locals import *

class Visualizer:
    def __init__(self, environ, zoom_level=1):
        '''Initializes the Visualizer
        zoom_level - must be an integer. It means that one agent will be shown as n x n pixel rectangle
        '''
        self.environ=environ
        self.map=self.environ.envmap
        self.zoom=zoom_level
        self.width=self.map.width
        self.height=self.map.height
        self.overall=True
        pygame.init()
        self.screen=pygame.display.set_mode((self.map.width * zoom_level, self.map.height * zoom_level))
        pygame.display.set_caption("Charteragents")
        background=pygame.Surface(self.screen.get_size()).convert()
        background.fill(Config.BACKGROUND_COLOR)
        self.screen.blit(background, (0, 0))
        pygame.display.flip()
        
    def handle_events(self):
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                exit()
            if event.type == KEYDOWN and event.key == K_SPACE:
                self.environ.activity=not self.environ.activity
            if event.type == MOUSEBUTTONDOWN:
                if self.overall:
                    x, y = pygame.mouse.get_pos()
                    x /= self.zoom
                    y /= self.zoom
                    agent = self.environ.get_agent((x, y))
                    if agent != None:
                        self.map = agent._map
                        pygame.display.set_caption("Charteragents - map of agent #%d" % agent.ID)
                        self.overall = False
                        self.redraw()
                else:
                    self.map = self.environ.envmap
                    pygame.display.set_caption("Charteragents")
                    self.overall = True
                    self.redraw()
        
    def redraw(self):
        self.handle_events()
        for x in xrange(0, self.width):
            for y in xrange(0, self.height):
                tiletype=self.map.get_tile((x, y)).tiletype
                tile=pygame.Surface((self.zoom, self.zoom)).convert()
                if tiletype == environ.Tile.MINE:
                    tile.fill(Config.MINE_COLOR)
                elif tiletype == environ.Tile.NORMAL:
                    tile.fill(Config.BACKGROUND_COLOR)
                elif tiletype == environ.Tile.UNMAPPED:
                    tile.fill(Config.UNMAPPED_COLOR)
                self.screen.blit(tile, (x*self.zoom, y*self.zoom))

        for a in self.environ.agents:
            x, y=a.coord
            agent=pygame.Surface((self.zoom, self.zoom)).convert()
            agent.fill(Config.AGENT_COLOR)
            self.screen.blit(agent, (x*self.zoom, y*self.zoom))
        pygame.display.flip()
        
        self.handle_events()
        time.sleep(Config.TURN_DURATION)
        self.handle_events()


# it's a pure module:		
if __name__ == "__main__":
    pass
